package com.scisoft.scan.control.pack;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.data.pack.PackBoxBean;
import com.scisoft.scan.data.pack.PackDetailBean;
import com.scisoft.scan.data.pack.PackInfoBean;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper_Pack;
import java.util.List;

public class PackOkDetailActivity extends Activity {

    private ImageView iv_Back; //返回按钮
    private ListView lv_data;
    private LinearLayout linearLayout;//标题头
    private TextView tv_statis;//箱号
    private TextView tv_info;//打包情况信息
    private TextView tv_customerPhone;//客户名称和电话
    private TextView tv_customerAdress;//客户地址
    private TextView tv_remark;//备注
    private TextView tv_opinfo;//操作员和操作时间

    private PackInfoBean packInfobean;//主界面客户信息
    private String flag;//当前订单的状态，从主表单带过来，注：1 未打包 2 打包中 3已打包
    //已经打包的配件单
    private List<PackBoxBean> packBoxBeanList;
    //当前箱号
    private int currentBox = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//即隐藏标题栏
        setContentView(R.layout.activity_pack_ok_detail);

        //参数传递
        Intent intent = getIntent();
        //客户信息
        packInfobean = (PackInfoBean) intent.getSerializableExtra("data");
        //订单状态
        flag = intent.getStringExtra("flag");

        //左侧页签组
        linearLayout = findViewById(R.id.layout_bar_group);
        //每页的内容
        lv_data = findViewById(R.id.lv_data);
        tv_statis = findViewById(R.id.tv_statis);
        tv_info = findViewById(R.id.tv_info);
        tv_info.setText(packInfobean.detail);
        tv_customerPhone = findViewById(R.id.tv_customerPhone);
        tv_customerPhone.setText("单    号：" + packInfobean.orderNo);
        tv_customerAdress = findViewById(R.id.tv_customerAdress);
        tv_customerAdress.setText("仓    库："+ packInfobean.depotName);
        tv_opinfo = findViewById(R.id.tv_opinfo);
        tv_opinfo.setText("打包人：" + packInfobean.operator);
        tv_remark = findViewById(R.id.tv_remark);
        tv_remark.setText("备    注：" + packInfobean.remark);
        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PackOkDetailActivity.this, PackInfoActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        //查询数据
        queryData();
    }

    //初始化界面数据
    private void queryData() {
        CommonUtil.showPress(this);
        QueryPackDetailTask queryPackInfoTask = new QueryPackDetailTask(PackOkDetailActivity.this);
        queryPackInfoTask.execute();
    }

    //设置tab页的颜色
    private void setButton() {
        List<PackDetailBean> packDetailBeans = packBoxBeanList.get(currentBox).packDetailBeanList;
        PackDetailAdapter adapter = new PackDetailAdapter("3", 2, packDetailBeans, PackOkDetailActivity.this);
        lv_data.setAdapter(adapter);
    }

    //绑定数据
    @SuppressLint("ResourceAsColor")
    private void BindBox(final Context context) {
        //选中最上面
        currentBox = packBoxBeanList.size() - 1;
        //清空
        linearLayout.removeAllViews();
        int width = linearLayout.getMeasuredWidth();
        //重新加载控件
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, width);
        int count = packBoxBeanList.size();
        for (int i = count - 1; i >= 0; i--) {
            TextView button = new TextView(context);
            button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            button.setGravity(Gravity.CENTER);
            button.setText(i + 1 + "");
            button.setId(i + 1);
            if (i == currentBox) {
                button.setBackground(getResources().getDrawable(R.drawable.edit_bg_button_state_white));
                button.setTextColor(getResources().getColor(R.color.bg_button));
            } else {
                button.setBackground(getResources().getDrawable(R.drawable.edit_bg_button_state_gray));
                button.setTextColor(getResources().getColor(R.color.black));
            }
            button.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("ResourceAsColor")
                @Override
                public void onClick(View v) {
                    TextView button = (TextView) v;
                    currentBox = Integer.parseInt(button.getText().toString()) - 1;
                    tv_statis.setText("箱号:" + packBoxBeanList.get(currentBox).packingNo);
                    button.setTextColor(getResources().getColor(R.color.bg_button));
                    button.setBackground(getResources().getDrawable(R.drawable.edit_bg_button_state_white));
                    int m = button.getId();
                    int buttonCount = packBoxBeanList.size();
                    for (int j = 0; j < buttonCount; j++) {
                        if (j + 1 != m) {
                            TextView tmpbutton = linearLayout.findViewById(j + 1);
                            tmpbutton.setBackground(getResources().getDrawable(R.drawable.edit_bg_button_state_gray));
                            tmpbutton.setTextColor(getResources().getColor(R.color.black));
                        }
                    }
                    setButton();
                }
            });
            linearLayout.addView(button, layoutParams);
        }

        if (currentBox < 0)
            currentBox = 0;

        tv_statis.setText("箱号:" + packBoxBeanList.get(currentBox).packingNo);

        setButton();
    }

    //查询打包信息
    class QueryPackDetailTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        QueryPackDetailTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                //packBoxBeanList = WebServiceHelper_Pack.PACK_09(packInfobean.customerId);//获取箱号列表
                packBoxBeanList = WebServiceHelper_Pack.PACK_03(packInfobean.orderId, flag);//获取箱号列表
                msg = "成功";
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindBox(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

}

